// ==========================================================================
// gin-sns自动生成业务逻辑层相关代码，只生成一次，按需修改,再次生成不会覆盖.
// date：2021-02-21 13:45:54 +0800 CST
// path: app/service/module/comment_service.go
// author：jon
// ==========================================================================
package module

import (
	"errors"
	"gin-sns/app/dao/module/comment"
	"gin-sns/app/model"
	"gin-sns/app/model/constants"
	commentModel "gin-sns/app/model/module/comment"
	"strings"
	"time"

	"github.com/gin-gonic/gin"

	"gin-sns/app/ginframe/utils/convert"
	"gin-sns/app/ginframe/utils/page"
)

var CommentService = newCommentService()

func newCommentService() *commentService {
	return &commentService{}
}

type commentService struct {
}

//根据主键查询数据
func (s *commentService) SelectRecordById(id int64) (*commentModel.Entity, error) {
	return comment.CommentDao.SelectById(id)
}

//
func (s *commentService) SelectRecordsIn(idarr []int64) ([]commentModel.Entity, error) {

	list, err := comment.CommentDao.FindIn("id", idarr)
	if list != nil && len(list) > 0 {
		err = errors.New("not found")
		list = nil
	}
	return list, err
}

//根据主键删除数据
func (s *commentService) DeleteRecordById(id int64) bool {
	return comment.CommentDao.DeleteById(id)
}

//批量删除数据记录
func (s *commentService) DeleteRecordByIds(ids string) int64 {
	ida := convert.ToInt64Array(ids, ",")
	result, err := comment.CommentDao.DeleteBatch(ida...)
	if err != nil {
		return 0
	}
	return result
}

//添加数据
func (s *commentService) AddSave(req *commentModel.AddReq, ctx *gin.Context) (int64, error) {
	var entity commentModel.Entity

	//entity.Id = req.Id
	entity.UserId = req.UserId
	entity.EntityType = req.EntityType
	entity.EntityId = req.EntityId
	entity.Content = req.Content
	entity.QuoteId = req.QuoteId
	entity.CountLike = req.CountLike
	entity.CountUnlike = req.CountUnlike
	entity.CountComments = req.CountComments
	entity.Status = req.Status
	entity.CreateTime = time.Now()
	//entity.CreateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user != nil {
	//	entity.CreateBy = user.LoginName
	//}

	_, err := comment.CommentDao.Insert(&entity)
	return entity.Id, err
}

//修改数据
func (s *commentService) EditSave(req *commentModel.EditReq, ctx *gin.Context) (int64, error) {
	entity := &commentModel.Entity{Id: req.Id}
	ok, err := comment.CommentDao.FindOne(entity)

	if err != nil {
		return 0, err
	}

	if !ok {
		return 0, errors.New("数据不存在")
	}

	entity.UserId = req.UserId
	entity.EntityType = req.EntityType
	entity.EntityId = req.EntityId
	entity.Content = req.Content
	entity.QuoteId = req.QuoteId
	entity.CountLike = req.CountLike
	entity.CountUnlike = req.CountUnlike
	entity.CountComments = req.CountComments
	entity.Status = req.Status
	//entity.UpdateTime = time.Now()
	//entity.UpdateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user == nil {
	//	entity.UpdateBy = user.LoginName
	//}

	return comment.CommentDao.Update(entity)
}

//根据条件查询数据
func (s *commentService) SelectListAll(params *commentModel.SelectPageReq) ([]commentModel.Entity, error) {
	return comment.CommentDao.SelectListAll(params)
}

//根据条件分页查询数据
func (s *commentService) SelectListByPage(params *commentModel.SelectPageReq) ([]commentModel.Entity, *page.Paging, error) {
	return comment.CommentDao.SelectListByPage(params)
}

// 导出excel
func (s *commentService) Export(param *commentModel.SelectPageReq) (string, error) {
	head := []string{"", "用户id", "实体类型", "实体id", "内容", "引用的comment的id", "赞同数量", "反对数量", "评论数量", "0未审核 1正常状态 2审核未通过 3删除", "创建时间", "更新时间"}
	col := []string{"id", "user_id", "entity_type", "entity_id", "content", "quote_id", "count_like", "count_unlike", "count_comments", "status", "create_time", "update_time"}
	return comment.CommentDao.SelectListExport(param, head, col)
}

// 发表评论
func (s *commentService) Publish(userId int64, form *commentModel.CreateCommentReq) (*commentModel.Entity, error) {
	form.Content = strings.TrimSpace(form.Content)

	if form.EntityType == 0 {
		return nil, errors.New("参数非法")
	}
	if form.EntityId <= 0 {
		return nil, errors.New("参数非法")
	}
	if len(form.Content) == 0 {
		return nil, errors.New("请输入评论内容")
	}
	entity := &commentModel.Entity{
		UserId:     userId,
		EntityType: form.EntityType,
		EntityId:   form.EntityId,
		Content:    form.Content,
		QuoteId:    form.QuoteId,
		Status:     model.CommentStatusOk,
		CreateTime: time.Now(),
	}

	_, err := comment.CommentDao.Insert(entity)
	if err != nil {
		return nil, err
	}

	// 更新帖子最后回复时间
	if form.EntityType == constants.EntityTypePost {
		PostService.UpdateCommentInfo(form.EntityId, time.Now().Unix())
	}

	// 发送消息
	MessageService.SendCommentMsg(entity)

	return entity, nil
}

// 统计数量
func (s *commentService) Count(entityType int, entityId int64) int64 {

	return comment.CommentDao.Count(entityType, entityId)

}

//add
func (s *commentService) IncrLikeCount(commentId int64) {
	comment.CommentDao.IncrLikeCount(commentId)
}

func (s *commentService) DecrLikeCount(commentId int64) {
	comment.CommentDao.DecrLikeCount(commentId)
}

func (s *commentService) IncrUnlikeCount(commentId int64) {
	comment.CommentDao.IncrUnlikeCount(commentId)
}

func DecrUnlikeCount(commentId int64) {
	comment.CommentDao.DecrUnlikeCount(commentId)
}

// 最近点赞
func (s *commentService) GetComments(entityType int, entityId int64, count int) []commentModel.Entity {
	list, err := comment.CommentDao.GetNewestComments(entityType, entityId, count)
	if err != nil {
		return nil
	}

	return list
}
